package com.twotiger.and.activity.account.investtransfer;

import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.os.Build;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;
import app.twotiger.p2p.R;
import app.twotiger.p2p.R.color;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.twotiger.and.Constants;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.activity.base.BaseHandler;
import com.twotiger.and.activity.base.SubTitle;
import com.twotiger.and.adapter.InvestTransferAdapter;
import com.twotiger.and.bean.Basebean;
import com.twotiger.and.bean.DataList;
import com.twotiger.and.bean.DebtCanTrans;
import com.twotiger.and.bean.DebtHasBuy;
import com.twotiger.and.bean.DebtTransed;
import com.twotiger.and.bean.DebtTransing;
import com.twotiger.and.util.ListUtils;
import com.twotiger.and.util.PreferencesUtils;
import com.twotiger.and.util.StringUtils;
import com.twotiger.and.util.TimeUtils;
import com.twotiger.and.util.ViewUtils;
import com.view.pulltorefresh.MyListView;
import com.view.pulltorefresh.PullToRefreshBase;
import com.view.pulltorefresh.PullToRefreshBase.OnLastItemVisibleListener;
import com.view.pulltorefresh.PullToRefreshBase.OnRefreshListener2;
import com.view.pulltorefresh.PullToRefreshMyListView;

/**
 * 
 * @Description: 
 * 投资转让
 * @Author gao
 * @Date 2015年8月12日 下午1:58:05
 */
@SuppressLint("ResourceAsColor")
public class InvestTransferPage extends BaseActivity implements OnClickListener {

    private PullToRefreshMyListView invest_transf_refresh_list;
    private View no_content_rl;
    private static final String TAG_refresh = "DealCouponPage_TAG_refresh";

    public static final String CANTRANS = "kzr";
    public static final String TRANSING = "zrz";
    public static final String TRANSED = "yzr";
    public static final String HASBUY = "ygm";

    private final int GETDEBTTRANSSUCCED = 0;
    private final int GETDEBTTRANSFAIL = 1;
    protected static final int EXCHANGECOUPONFAIL = 3;

    private String type;

    private int pNum = 1;
    private HashMap<String, String> parms;

    private SubTitle subTitle;
    private ImageView iv_no_content;

    private View header_view; //头布局
    private InvestTransferAdapter mInvestTransferAdapter;
    private RadioButton cantrans_btn ,transing_btn , completetrans_btn , hasbuy_btn;
    private ImageView iv_1,iv_2,iv_3,iv_4;

    @SuppressLint("InflateParams")
    @Override
    protected View loadViewLayout(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.invest_transfer_layout, null);
        header_view = inflater.inflate(R.layout.invest_trans_list_header, null);
        return view;
    }

    @Override
    protected void findViewById(View view) {
        invest_transf_refresh_list = (PullToRefreshMyListView) view
            .findViewById(R.id.invest_transf_refresh_list);
        no_content_rl = (View) view.findViewById(R.id.coupon_no_content_rl);
        iv_no_content = (ImageView) view.findViewById(R.id.iv_no_content);

        cantrans_btn = (RadioButton) view.findViewById(R.id.cantrans_btn);
        iv_1 = (ImageView) view.findViewById(R.id.iv_1);
        transing_btn = (RadioButton) view.findViewById(R.id.transing_btn);
        iv_2 = (ImageView) view.findViewById(R.id.iv_2);
        completetrans_btn = (RadioButton) view.findViewById(R.id.completetrans_btn);
        iv_3 = (ImageView) view.findViewById(R.id.iv_3);
        hasbuy_btn = (RadioButton) view.findViewById(R.id.hasbuy_btn);
        iv_4 = (ImageView) view.findViewById(R.id.iv_4);

        initIdOfHeader();

        subTitle = new SubTitle(view) {
            @Override
            protected void clickTitle(View v) {
            }

            @Override
            protected void clickRightBtn(View v) {
            }

            @Override
            protected void cickLeftBtn(View v) {
                onBackPress();
            }
        };
        subTitle.setTitle("投资转让");
        subTitle.hideBtns();
        subTitle.title_left_bt.setVisibility(View.VISIBLE);
    }

    /**
     * 增加头布局
     */
    private void addHeaderForList() {
        
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB){
            return;
        }
        
        ListView lv = invest_transf_refresh_list.getRefreshableView();
        lv.addHeaderView(header_view);
    }
    
    /**
     * 将listview的header给他remove掉
     */
    private void removeHeaderForList() {
        ListView lv = invest_transf_refresh_list.getRefreshableView();
        lv.removeHeaderView(header_view);
    }

    private void initIdOfHeader() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

        case R.id.cantrans_btn:
            pNum = 1;
            type = CANTRANS;
            cantrans_btn.setTextColor(getResources().getColor(color.orange));
            transing_btn.setTextColor(getResources().getColor(color.home_bottom));
            completetrans_btn.setTextColor(getResources().getColor(color.home_bottom));
            hasbuy_btn.setTextColor(getResources().getColor(color.home_bottom));
            iv_1.setVisibility(View.VISIBLE);
            iv_2.setVisibility(View.GONE);
            iv_3.setVisibility(View.GONE);
            iv_4.setVisibility(View.GONE);

            getinvesttranslist();
            break;
        case R.id.transing_btn:
            pNum = 1;
            type = TRANSING;
            cantrans_btn.setTextColor(getResources().getColor(color.home_bottom));
            transing_btn.setTextColor(getResources().getColor(color.orange));
            completetrans_btn.setTextColor(getResources().getColor(color.home_bottom));
            hasbuy_btn.setTextColor(getResources().getColor(color.home_bottom));
            iv_1.setVisibility(View.GONE);
            iv_2.setVisibility(View.VISIBLE);
            iv_3.setVisibility(View.GONE);
            iv_4.setVisibility(View.GONE);

            getinvesttranslist();
            break;
        case R.id.completetrans_btn://已完成
            pNum = 1;
            type = TRANSED;
            cantrans_btn.setTextColor(getResources().getColor(color.home_bottom));
            transing_btn.setTextColor(getResources().getColor(color.home_bottom));
            completetrans_btn.setTextColor(getResources().getColor(color.orange));
            hasbuy_btn.setTextColor(getResources().getColor(color.home_bottom));
            iv_1.setVisibility(View.GONE);
            iv_2.setVisibility(View.GONE);
            iv_3.setVisibility(View.VISIBLE);
            iv_4.setVisibility(View.GONE);

            getinvesttranslist();
            break;
        case R.id.hasbuy_btn://已购买
            pNum = 1;
            type = HASBUY;
            cantrans_btn.setTextColor(getResources().getColor(color.home_bottom));
            transing_btn.setTextColor(getResources().getColor(color.home_bottom));
            completetrans_btn.setTextColor(getResources().getColor(color.home_bottom));
            hasbuy_btn.setTextColor(getResources().getColor(color.orange));
            iv_1.setVisibility(View.GONE);
            iv_2.setVisibility(View.GONE);
            iv_3.setVisibility(View.GONE);
            iv_4.setVisibility(View.VISIBLE);

            getinvesttranslist();
            break;
        case R.id.coupon_no_content_rl:
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            getinvesttranslist();
            break;
        default:
            break;
        }
    }

    /** 
     */
    @SuppressLint("HandlerLeak")
    @Override
    protected void setListener() {
        handler = new BaseHandler(this) {
            private AlertDialog showCustomDialog;

            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case GETDEBTTRANSSUCCED:
                    Basebean result = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    if (result.isOk()) {
                        invest_transf_refresh_list.getLoadingLayoutProxy().setLastUpdatedLabel(
                            "最后更新：今天 " + TimeUtils.getFormatDate(" HH:mm"));
                        DataList dataList = JSON.parseObject(result.data, DataList.class);
                        List list = null;
                        if (CANTRANS.equals(type)) {
                            removeHeaderForList();
                            addHeaderForList();
                            list = JSON.parseArray(dataList.getList(), DebtCanTrans.class);
                        } else if (TRANSING.equals(type)) {
                            removeHeaderForList();
                            list = JSON.parseArray(dataList.getList(), DebtTransing.class);
                        } else if (TRANSED.equals(type)) {
                            removeHeaderForList();
                            list = JSON.parseArray(dataList.getList(), DebtTransed.class);
                        } else if (HASBUY.equals(type)) {
                            removeHeaderForList();
                            list = JSON.parseArray(dataList.getList(), DebtHasBuy.class);
                        }
                        if (!ListUtils.isEmpty(list)) {
                            if(list.size() < Integer.parseInt(Constants.pageSize)){
                                invest_transf_refresh_list.changeMyFooterViewContent(false);
                            }else{
                                invest_transf_refresh_list.changeMyFooterViewContent(true);
                            }
                            no_content_rl.setVisibility(View.GONE);
                            invest_transf_refresh_list.setVisibility(View.VISIBLE);
                            if (pNum == 1) {
                                mInvestTransferAdapter.resetData(list);
                            } else {
                                mInvestTransferAdapter.addData(list);
                            }
                            mInvestTransferAdapter.notifyDataSetChanged();
                        } else {
                            //请求数据是空的
                            if (pNum == 1) {
                                no_content_rl.setVisibility(View.VISIBLE);
                                invest_transf_refresh_list.setVisibility(View.GONE);
                                iv_no_content.setImageResource(R.drawable.no_content);
                            } else {
                                invest_transf_refresh_list.changeMyFooterViewContent(false);
                                mInvestTransferAdapter.notifyDataSetChanged();
                            }
                        }
                    } else {
                        showToast(result.codeDesc);
                    }
                    break;
                case GETDEBTTRANSFAIL:
                    no_content_rl.setVisibility(View.VISIBLE);
                    invest_transf_refresh_list.setVisibility(View.GONE);
                    break;
                default:
                    break;
                }
                invest_transf_refresh_list.onRefreshComplete();
                super.handleMessage(msg);
            }

        };
        
        invest_transf_refresh_list.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

            @Override
            public void onLastItemVisible() {
                String footerContent = ((TextView)invest_transf_refresh_list.getChildviewOfFooterview()).getText().toString();
                if("已无更多项目".equals(footerContent)){
                    return;
                }
                /**
                 * 加载更多数据
                 */
                 pNum++;
                 getinvesttranslist();
            }
        });

        invest_transf_refresh_list.setOnRefreshListener(new OnRefreshListener2<MyListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<MyListView> refreshView) {
                /**
                * 刷新数据
                */
                pNum = 1;
                getinvesttranslist();

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<MyListView> refreshView) {
                /**
                * 加载更多数据
                */
                pNum++;
                getinvesttranslist();
            }
        });


        no_content_rl.setOnClickListener(this);
        cantrans_btn.setOnClickListener(this);
        transing_btn.setOnClickListener(this);
        completetrans_btn.setOnClickListener(this);
        hasbuy_btn.setOnClickListener(this);
    }

    private void getinvesttranslist() {
        parms.clear();
        parms.put("token", getToken());
        parms.put("pageNum", String.valueOf(pNum));
        parms.put("pageSize", Constants.pageSize);
        parms.put("type", type);
        parms.put("timestamp", TimeUtils.getTimeStamp());
        getDate(parms, Constants.INVESTBONDQUERY, handler, GETDEBTTRANSSUCCED, GETDEBTTRANSFAIL,
            true, true, false);
    }

    @Override
    protected void processLogic() {
        parms = new HashMap<String, String>();
        invest_transf_refresh_list.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        type = StringUtils.isEmpty(getIntent().getStringExtra("TYPE")) ? CANTRANS : getIntent().getStringExtra("TYPE");
        if(HASBUY.equals(type)){
            pNum = 1;
            type = HASBUY;
            cantrans_btn.setTextColor(getResources().getColor(color.home_bottom));
            transing_btn.setTextColor(getResources().getColor(color.home_bottom));
            completetrans_btn.setTextColor(getResources().getColor(color.home_bottom));
            hasbuy_btn.setTextColor(getResources().getColor(color.orange));
            iv_1.setVisibility(View.GONE);
            iv_2.setVisibility(View.GONE);
            iv_3.setVisibility(View.GONE);
            iv_4.setVisibility(View.VISIBLE);

            getinvesttranslist();
        }
        initData();
    }

    public void initData() {
        mInvestTransferAdapter = new InvestTransferAdapter(InvestTransferPage.this);
        invest_transf_refresh_list.setAdapter(mInvestTransferAdapter);
        getinvesttranslist();
    }

    @Override
    protected void onBackPress() {
        finish();
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

}
